<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实例图 - 解决问题</title>
</head>

<body>


    <script src="https://cdn.jsdelivr.net/npm/echarts@5.4.0/dist/echarts.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/Mock.js/1.0.1-beta3/mock-min.js"></script>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <!-- 为 ECharts 准备一个定义了宽高的 DOM -->
    <div id="Application">
        <button type="button" @click="clickRefresh">本周数据</button>
        <button type="button" @click="clickRefresh">上周数据</button>

        <div style="width: 600px; height: 400px" ref="refChart"></div>
    </div>

    <script type="text/javascript">
        const App = Vue.createApp({
            setup() {

                //生成数据
                let Random = Mock.Random;
                let getChartsData = () => {
                    let array = [];
                    for (let i = 0; i < 7; i++) {
                        array.push(Random.integer(0, 100))
                    }
                    console.log("生成的数据：", array)
                    return array;
                }


                //展示图表
                //	此dom为echarts图标展示dom
                //获取dom节点
                const refChart = Vue.ref();
                //定义全局变量
                const chart = Vue.ref();
                //展示表格
                const showChat = () => {
                    //判断 dom 是否为空或未定义，如果是则调用 dispose() 方法销毁，再初始化 echarts.init
                    if (chart.value != null && chart.value != "" && chart.value != undefined) {
                        chart.value.dispose();
                    }

                    // 基于准备好的dom，初始化echarts实例
                    chart.value = echarts.init(refChart.value);

                    // 指定图表的配置项和数据
                    const option = {
                        title: {
                            text: "ECharts 入门示例",
                        },
                        tooltip: {},
                        legend: {
                            data: ["作者一"],
                        },
                        xAxis: {
                            data: ["周一", "周二", "周三", "周四", "周五", "周六", "周七"],
                        },
                        yAxis: {},
                        series: [
                            {
                                name: "作者一",
                                type: "bar",
                                data: getChartsData(),//获取随机生成的数据
                            },
                        ],
                    };
                    // 使用刚指定的配置项和数据显示图表。
                    chart.value.setOption(option);
                }



                //点击一次就生成一个表格
                const clickRefresh = () => {
                    showChat()

                }
                Vue.onMounted(() => {
                    showChat();//首次加载
                });
                return {
                    refChart,//获取节点用
                    clickRefresh,
                };
            },
        });
        App.mount("#Application");
    </script>
</body>

</html>